package com.hjl.sr.util;

import javax.servlet.http.HttpServletRequest;

/**
 * @author pister 2011-12-28 02:40:33
 */
public class ServletUtil {

	public static String getServletPath(HttpServletRequest request) {
		final String pathInfo = request.getPathInfo();
		if (pathInfo != null && pathInfo.length() > 0) {
			return pathInfo;
		}
        String servletPath = request.getServletPath();
        String requestUri = request.getRequestURI();
        if (requestUri != null && servletPath != null && !requestUri.endsWith(servletPath)) {
            int pos = requestUri.indexOf(servletPath);
            if (pos > -1) {
                servletPath = requestUri.substring(requestUri.indexOf(servletPath));
            }
        }
        if (null != servletPath && !"".equals(servletPath)) {
            return servletPath;
        }
        int startIndex = request.getContextPath().equals("") ? 0 : request.getContextPath().length();
        int endIndex = pathInfo == null ? requestUri.length() : requestUri.lastIndexOf(pathInfo);

        if (startIndex > endIndex) { // this should not happen
            endIndex = startIndex;
        }

        return requestUri.substring(startIndex, endIndex);
    }
	

}
